package com.rd.student.ruanjian.class4.lichunquan.Text02;


import java.util.Scanner;

public class Test11 {
    //井字游戏
    /*
    * 玩家手动录入位置
    */
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String[][] checkerboard = {{"0,0","0,1","0,2"},{"1,0","1,1","1,2"},{"2,0","2,1","2,2"}};
        int i = 0;
        printOfCheckerBoard(checkerboard);
        while(i<9){
            int x ;
            int y ;
            //下棋
            if( i==0||i%2==0 ){
                System.out.println("p1输入要下的位置");
                x = sc.nextInt();
                y = sc.nextInt();
                if( x > 2||x < 0||y>2|| y < 0||checkerboard[x][y].equals(" X ") || checkerboard[x][y].equals(" O ") ){
                    System.out.println("重下");
                    i--;
                }else{
                    checkerboard[x][y] = " X ";
                    if(printOfCheckerBoard(checkerboard) == false) {
                        System.out.println("游戏结束，p1获胜");
                        break;
                    }
                }
            }else{
                System.out.println("p2输入要下的位置");
                x = sc.nextInt();
                y = sc.nextInt();
                if(x > 2||x < 0||y>2|| y < 0|| checkerboard[x][y].equals(" X ") || checkerboard[x][y].equals(" O ") ){
                    System.out.println("重下");
                    i--;
                }else{
                    checkerboard[x][y] = " O ";
                    if(printOfCheckerBoard(checkerboard) == false) {
                        System.out.println("游戏结束，p2获胜");
                        break;
                    }
                }
            }
            i++;
        }
        //复盘
        System.out.println("---------------");
        for (int j = 0; j < checkerboard.length; j++) {
            for (int k = 0; k < checkerboard[j].length; k++) {
                System.out.print("|" + checkerboard[j][k] + "|");
            }
            System.out.println();
        }
        System.out.println("---------------");
    }
    public static boolean printOfCheckerBoard(String[][] checkerboard){
        //斜向判断
        if( ( checkerboard[0][0].equals(checkerboard[1][1]) && checkerboard[1][1].equals(checkerboard[2][2]) )
                ||
                ( checkerboard[2][0].equals(checkerboard[1][1]) && checkerboard[1][1].equals(checkerboard[0][2]) )  ){
            System.out.println("游戏结束");
            return false;
        }
        System.out.println("---------------");
        for (int i = 0; i < checkerboard.length; i++) {
            //横判断
            if(checkerboard[i][0].equals(checkerboard[i][1]) && checkerboard[i][1].equals(checkerboard[i][2])) {
                System.out.println("游戏结束");
                return false;
            }
            for (int j = 0; j < checkerboard[i].length; j++) {
                //竖判断
                if(checkerboard[0][j].equals(checkerboard[1][j]) && checkerboard[1][0].equals(checkerboard[2][0])){
                    System.out.println("游戏结束");
                    return false;
                }
                System.out.print("|" + checkerboard[i][j] + "|");
            }
            System.out.println();
        }
        System.out.println("---------------");
        return true;
    }
}
